Write a Rust function to reverse the order of elements in a vector.
Write a Rust function to reverse the order of elements in a vector.
213
20-Jun-2023
Aryan Kumar
24-Jun-2023Sure, here is a Rust function to reverse the order of elements in a vector:
Rust
This function first initializes two iterators, one at the beginning of the vector and one at the end. The function then loops while the iterators haven't crossed. In each iteration of the loop, the function swaps the elements at the two iterators. The function then increments i and decrements j. Finally, the function prints the vector.
To run this program, you can save it as a
.rsfile and then compile and run it using the following commands:Code snippet
This will print the following output:
Code snippet
This indicates that the order of the elements in the vector has been reversed.